package com.neusoft.boot_ssm_crud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.boot_ssm_crud.dao.mapper.UserMapper;
import com.neusoft.boot_ssm_crud.domain.User;
import com.neusoft.boot_ssm_crud.domain.UserExample;
import com.neusoft.boot_ssm_crud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int deleteByExample(String username,UserExample example)
    {
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andUserNameEqualTo(username);
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public User selectByPrimaryKey(Long userId) {

        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<User> selectByExample(UserExample example)
    {
        return userMapper.selectByExample(example);
    }

    @Override
    public int updateByExample(User record) {
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andUserIdEqualTo(record.getUserId());
        return userMapper.updateByExample(record,userExample);
    }

    @Override
    public PageInfo<User> queryUserByPage(int pageNo, int PageSize) {
        Page<User> page = PageHelper.startPage(pageNo, PageSize,"user_id,age ASC");
        UserExample userExample = new UserExample();
        userExample.setDistinct(true);
        userExample.createCriteria()
                .andAgeIsNotNull()
                .andPasswordIsNotNull();
        //" AND user_id = 1"
        List<User> list = userMapper.selectByExample(userExample);
        return page.toPageInfo();
    }
}
